<script setup>
import { ref, onMounted } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import { artDelChannelServices, artGetChannelServices } from '@/api/article.js'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from '@/views/article/components/ChannelEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const cate = ref([])
const loading = ref(false)

const dialog = ref()

const getCate = async () => {
  loading.value = true
  let response = await artGetChannelServices()
  console.log(response)
  cate.value = response.data.data
  loading.value = false
}
onMounted(() => {
  getCate()
})
const editHandle = (row, index) => {
  console.log(row, index)
  dialog.value.open(row)
}
const delHandle = async (row) => {
  console.log(row.id)
  // 在确认消息上要加await，会正常等待用户确认，否则会直接默认执行确认按钮
  await ElMessageBox.confirm('确定要删除该分类吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDelChannelServices(row.id)
  ElMessage.success('删除成功')
  getCate()
}
const addChannel = () => {
  dialog.value.open({})
}
</script>

<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button type="primary" @click="addChannel">新增分类</el-button>
    </template>
    <el-table :data="cate" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            @click="editHandle(scope.row, scope.$index)"
          ></el-button>
          <el-button
            type="warning"
            :icon="Delete"
            circle
            @click="delHandle(scope.row, scope.$index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @updateCate="getCate"></ChannelEdit>
  </PageContainer>
</template>

<style scoped></style>
